package Store;

import Auxiliary.Sequence_StateObserver;
import DataView.Expr;
import DataView.Sequence_Error;
import DataView.StateObserverInfo;
import Visualization.StateObserverInvVis;

public class StateObserverInv extends Element 
{
	public Sequence_StateObserver list_stateObserver = new Sequence_StateObserver();

	public StateObserverInv()
	{
		StateObserver ini = new StateObserver("initial", Type.boolean_(), new Expr("not final"));
		list_stateObserver.add(ini);
		StateObserver fin = new StateObserver("final",Type.boolean_(),new Expr("not initial"));
		list_stateObserver.add(fin);
	}
	
	public Sequence_Error check( EventCondReact e) 
	{
		Sequence_Error lista = new Sequence_Error();
		for (StateObserver s : list_stateObserver.getAll())
		{
			int i=0;
			for (StateObserver n : list_stateObserver.getAll())
				if (s.getName().equals(n.getName()))
						i++;
				if(i>1){
					lista.add("There are  Two or more StateObservers with the name "+s.getName());
					break;
				}
		}
		for (StateObserver s : list_stateObserver.getAll())
		{
			int i=0;
			for (Event x : e.events.getAll())
				if (s.getName().equals(x.getName()))
					i++;
			if(i>0){
				lista.add("There are a StateObserver and an Event whit the same name "+s.getName());
				break;
			}
			}
		return lista;
	}

	public void addStateObserver(StateObserver aSo) 
	{
		list_stateObserver.add(aSo);
	}

	public void addStateObservers(Sequence_StateObserver aSo) 
	{
		int max=0;
		if(aSo!=null) max = aSo.size();
		for(int i= 0 ; i<max ; i++)
			addStateObserver(aSo.get(i));
	}

	public void deleteStateObserver(StateObserver aSo) 
	{
		list_stateObserver.delete(aSo);
	}

	public void updateSO(StateObserver aSo, StateObserverInfo aNewSoInfo) 
	{
		int max=0;
		if(list_stateObserver!= null)
			max = list_stateObserver.size();
		for(int i=0; i<max; i++)
		{
			if(list_stateObserver.get(i)==aSo)
				list_stateObserver.get(i).mkElement(aNewSoInfo);
		}
	}

	public Sequence_StateObserver getAll()
	{
		return list_stateObserver;
	}
	public StateObserverInvVis vis() 
	{
		return list_stateObserver.vis();
	}
}